﻿using System.Globalization;
using KinonekoSoftware.Foundation.UI.Media;

namespace KinonekoSoftware.Foundation.UI.Converters
{
    
    public class ColorToRGBConverter : IValueConverter
    {
        private const    string Format  = "R:{0}, G:{1}, B:{2}";
        private const    string Format2 = "A:{0}, R:{1}, G:{2}, B:{3}";
        private readonly bool   _displayAlphaChannel;
        
        public ColorToRGBConverter(bool displayAlphaChannel)
        {
            _displayAlphaChannel = displayAlphaChannel;
        }


        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is not Color c)
            {
                c = MediaFactory.ToColor(value?.ToString());
            }

            return _displayAlphaChannel ?
                string.Format(Format2, c.A, c.R, c.G, c.B) :
                string.Format(Format, c.R, c.G, c.B);
        }
        
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
}